package com.tsd.base.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tsd.base.entity.BaseCodeRule;
import com.tsd.base.entity.BaseCodeRuleExt;
import com.tsd.base.service.BaseCodeRuleService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.controller.SystemBaseController;
import com.tsd.system.entity.SysUser;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：编码规则主表 控制层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class BaseCodeRuleController extends SystemBaseController {

    @Resource
    private BaseCodeRuleService baseCodeRuleService;

    /**
     * 根据ID获取编码规则主表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getBaseCodeRuleById.action", method = RequestMethod.POST)
    public HttpResult<BaseCodeRule> getBaseCodeRuleById() {
        HttpResult<BaseCodeRule> hr = new HttpResult<BaseCodeRule>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            BaseCodeRule baseCodeRule = baseCodeRuleService.getBaseCodeRuleById(id);
            hr.setData(baseCodeRule);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 根据ID删除编码规则主表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteBaseCodeRule.action", method = RequestMethod.POST)
    public HttpResult<String> deleteBaseCodeRule() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            baseCodeRuleService.deleteBaseCodeRule(id, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    /**
     * 保存编码规则主表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveBaseCodeRule.action", method = RequestMethod.POST)
    public HttpResult<BaseCodeRule> saveBaseCodeRule() {
        HttpResult<BaseCodeRule> hr = new HttpResult<BaseCodeRule>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            String json = JSON.toJSONString(map);
            BaseCodeRuleExt t = JSON.parseObject(json, BaseCodeRuleExt.class);
            baseCodeRuleService.saveBaseCodeRule(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
            hr.setData(t);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 分页查询编码规则主表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findBaseCodeRules.action", method = RequestMethod.POST)
    public HttpResult<List<BaseCodeRuleExt>> findBaseCodeRules() {
        HttpResult<List<BaseCodeRuleExt>> hr = new HttpResult<List<BaseCodeRuleExt>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyIntegerValue(map, params, "state", "state", false, 1);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true, false);
            PageBean page = new PageBean(map);
            List<BaseCodeRuleExt> list = baseCodeRuleService.findBaseCodeRules(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 批量删除编码规则主表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchDeleteBaseCodeRule.action", method = RequestMethod.POST)
    public HttpResult<String> batchDeleteBaseCodeRule() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object idObject = map.get("id_list");
            super.checkEmpty(idObject, "请提供参数id_list");
            if (idObject instanceof List) {
                List<Long> idList = JSONArray.parseArray(JSON.toJSONString(idObject), Long.class);
                baseCodeRuleService.batchDeleteBaseCodeRule(idList, opUser);
            } else {
                throw new HlpException("id_list非数组格式");
            }
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 获取单据编号
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAutoOrderCode.action", method = RequestMethod.POST)
    public HttpResult<String> getAutoOrderCode() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            String rule_code = HlpUtils.getString(map, "rule_code");
            String rule_name = HlpUtils.getString(map, "rule_name");
            super.checkEmpty(rule_code, "请提供rule_code！");
            String code = baseCodeRuleService.getNextCode(rule_code, rule_name, map);
            hr.setData(code);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


}
